package com.feihao.zhou.web.controller.system;

import com.feihao.zhou.bean.system.Dept;
import com.feihao.zhou.bean.system.User;
import com.feihao.zhou.service.system.DeptService;
import com.feihao.zhou.service.system.Impl.DeptServiceImpl;
import com.feihao.zhou.service.system.Impl.UserServiceImpl;
import com.feihao.zhou.service.system.UserService;
import com.feihao.zhou.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/user")
public class UserServlet extends HttpServlet {
    static UserService userService = new UserServiceImpl();
    static DeptService deptService = new DeptServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text.html;charset=utf-8");

        //获取请求option：
        String operation = req.getParameter("operation");
        if ("list".equals(operation)) {
            //查询方法：
            findAll(req, resp);
        } else if ("toAdd".equals(operation)) {
            add(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            update(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        userService.delete(id);
        //重定向到查询：
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");
        userService.update(user);


        //重定向到查询：
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User user = userService.findById(id);

        req.setAttribute("user",user);
        //跳转之前，先查询所有的部门添加到add,jsp:
        List<Dept> all = deptService.findAll();

        req.setAttribute("deptList",all);
        req.getRequestDispatcher("/WEB-INF/system/user/update.jsp").forward(req,resp);


    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");
        userService.save(user);


        //重定向到查询：
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //跳转之前，先查询所有的部门添加到add,jsp:
        List<Dept> all = deptService.findAll();

        req.setAttribute("deptList",all);
        req.getRequestDispatcher("/WEB-INF/system/user/add.jsp").forward(req,resp);
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 5;
        String pageStr = req.getParameter("page");
        String sizeStr = req.getParameter("size");
        if (StringUtils.isNotBlank(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(sizeStr)) {
            size = Integer.parseInt(sizeStr);
        }
        PageInfo<User> all = userService.findAll(page, size);

        //将数据转发到页面：
        req.setAttribute("page", all);
        req.getRequestDispatcher("/WEB-INF/system/user/list.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
